package com.coocaa.x.service.liteservice;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.IBinder;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.service.XServiceManager.XServiceConnection;
import com.coocaa.x.service.liteservice.aidl.ILiteService;

public class LiteServiceConnecter extends XServiceConnection<ILiteService> {
    private static final int VERSION = 4;

    public static final int getVersion() {
        return VERSION;
    }

    public static boolean setEnable(Context context, boolean val) {
        int state = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        if (val)
            state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;

        try {
//            Android.setComponentEnabledSetting(context, getComponentName(context), state);
//            check(context, getComponentName(context), state);
//            Android.setComponentEnabledSetting(context, LiteDBConnecter.getComponentName(context),
//                    state);
//            check(context, LiteDBConnecter.getComponentName(context),
//                    state);
//            Android.setComponentEnabledSetting(context,
//                    LiteCacheConnecter.getComponentName(context), state);
//            check(context,
//                    LiteCacheConnecter.getComponentName(context), state);
//            Android.setComponentEnabledSetting(context,
//                    LiteCCConnectorConnecter.getComponentName(context), state);
//            check(context,
//                    LiteCCConnectorConnecter.getComponentName(context), state);
//            Android.setComponentEnabledSetting(context,
//                    LiteDaemonConnecter.getComponentName(context), state);
//            check(context,
//                    LiteDaemonConnecter.getComponentName(context), state);
//            Android.setComponentEnabledSetting(context,
//                    LiteUserConnecter.getComponentName(context), state);
//            check(context,
//                    LiteUserConnecter.getComponentName(context), state);
////            Android.setComponentEnabledSetting(context,
////                    LiteEntryServiceConnecter.getComponentName(context), state);
//            Android.setComponentEnabledSetting(context,
//                    LitePlayerClientServiceConnecter.getComponentName(context), state);
//            check(context,
//                    LitePlayerClientServiceConnecter.getComponentName(context), state);
//            Android.setComponentEnabledSetting(context,
//                    LitePlayerKernelServiceConnecter.getComponentName(context), state);
//            check(context,
//                    LitePlayerKernelServiceConnecter.getComponentName(context), state);
//            Android.setComponentEnabledSetting(context,
//                    LitePushConnector.getComponentName(context), state);
//            check(context,
//                    LitePushConnector.getComponentName(context), state);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private static void check(Context context, ComponentName cn, int state) {
        while (state != Android.getComponentEnabledSetting(context, cn))
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }

    public static final String ACTION = "com.coocaa.x.service.lite2";
    private static final String CLASS_NAME = "com.coocaa.x.service.liteservice.LiteService";

    public static final ComponentName getComponentName(Context context) {
        return new ComponentName(context, CLASS_NAME);
    }

    public static final LiteServiceConnecter connecter = new LiteServiceConnecter();

    private ILiteService service = null;

    public LiteServiceConnecter() {
        super(ACTION);
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder s) {
        service = (ILiteService) ILiteService.Stub.asInterface(s);
        super.onServiceConnected(name, s);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        service = null;
        super.onServiceDisconnected(name);
        //如果entryservice已绑定的liteservice挂掉，则重新绑定
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                bindService(mContext, mContext.getPackageName());
            }
        });
    }

    @Override
    protected ILiteService _getService() {
        return service;
    }

}